home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / MYIODEMO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-14  |  4KB  |  157 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 06-13-93 (12:24)             Number: 142
  4. From: DAVID NUGENT                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: [11 of 12] Myiodemo.cpp        Conf: (37) C++ Langua
  7. ---------------------------------------------------------------------------
  8. // Myiodemo.cpp
  9. // This is a trivial program which uses the myio loopback class
  10. // to demonstrate the basics on writing an io interface using
  11. // the AT&T C++ iostream classes.
  12. // The program simply provides the ability to selectively add
  13. // to or read from a Myio instance and display information to
  14. // assist in understanding how it all works.
  15. //
  16.  
  17. # include "Mystream.h"      // This includes Myio.h and iostream.h
  18. # include "myLine.h"
  19. # include <conio.h>         // For getch()
  20. # include <ctype.h>         // For toupper()
  21. # include <string.h>
  22.  
  23. # define NL char('\n')
  24.  
  25.     // Let's do the "application is a class" trick
  26.  
  27. class myApplication
  28. {
  29.             // Defines a pointer to member function type
  30.             // used for dispatching the menu
  31.  
  32.     typedef void (myApplication::*pvf) (void);
  33.  
  34.   public:
  35.  
  36.     myApplication (void) : mio() {}
  37.     int execute (void);
  38.  
  39.   private:
  40.  
  41.     iostream & stream (void) { return mio.stream(); }
  42.     int domenu (void);
  43.     void send (void);
  44.     void read (void);
  45.     void disp (void);
  46.     void peek (void);
  47.     void flsh (void);
  48.     void stat (void);
  49.  
  50.     pvf choice;     // Function called to execute
  51.     Myio mio;       // IO object
  52.  
  53. };
  54.  
  55.  
  56. void
  57. myApplication::disp (void)
  58. {
  59.     cout << "Mystream status:" << NL
  60.          << "Chrs in output buffer = " << stream().rdbuf()->out_waiting() << NL
  61.          << "Chrs in  input buffer = " << stream().rdbuf()->in_avail()    << NL
  62.          << "Myio object status = "
  63.             << mio.count() << char('/') << mio.size()
  64.             << " LastWrite=" << (mio.writeok() ? "OK" : "Incomplete")
  65.             << " LastRead=" << (mio.readok() ? "OK" : "EOF")
  66.          << endl;
  67. }
  68.  
  69.     // Request a line and send it to the IO device
  70.  
  71. void
  72. myApplication::send (void)
  73. {
  74.     cout << NL << "Enter text to write - press <ENTER> when done\n:";
  75.     myLine L;
  76.     cin >> L;
  77.     int l = strlen(L);
  78.     if (!l)
  79.         cerr << "Nothing entered." << endl;
  80.     else
  81.     {
  82.         cout << "Writing '"
  83.              << L
  84.              << char('\'')
  85.              << endl;
  86.         stream() << L << NL;    // Send the entered data, NL terminated
  87.         cout << "Chrs written to Myio object = " << (l + 1) << NL;
  88.         disp ();
  89.     }
  90. }
  91.  
  92. void
  93. myApplication::read (void)
  94. {
  95.     cout << NL << "Reading a line from object:" << NL;
  96.     myLine L;
  97.     mio.stream().clear();
  98.     mio.stream() >> L;
  99.     int l = strlen(L);
  100.     if (!l)
  101.     {
  102.         cout << "Nothing read." << endl;
  103.         mio.stream().clear();       // Clear EOF status
  104.     }
  105.     else
  106.     {
  107.         cout << "Read '"
  108.              << L
  109.              << char('\'')
  110.              << endl;
  111.         cout << "Chrs read from Myio object = " << (l + 1) << NL;
  112.         disp ();
  113.     }
  114. }
  115.  
  116. void
  117. myApplication::flsh (void)
  118. {
  119.     cout << NL << "Flushing stream" << endl;
  120.     stream() << flush;
  121.     disp ();
  122. }
  123.  
  124. void
  125. myApplication::stat (void)
  126. {
  127.     cout << NL << "Myio object buffer dump:" << NL;
  128.     mio.dump();
  129.     disp ();
  130.     stream().rdbuf()->dbp();    // Dump stream info
  131. }
  132.  
  133. int
  134. myApplication::domenu (void)
  135. {
  136.     cout << NL
  137.          << "W)rite  R)ead  D)ump  F)lush  Q)uit\n"
  138.          << "Select: "
  139.          << flush;      // Need to flush here for portability
  140.     int key;
  141.     for (;;)
  142.     {
  143.         key = getch ();
  144.         switch (toupper(key))
  145.         {
  146.         case 'W': choice = &myApplication::send;    break;
  147.         case 'R': choice = &myApplication::read;    break;
  148.         case 'D': choice = &myApplication::stat;    break;
  149.         case 'F': choice = &myApplication::flsh;    break;
  150.         case 'Q': key = 0;                          break;
  151.         default:
  152.             continue;
  153.         }
  154.         cout << char(key) << endl;
  155.         break;
  156.     }
  157.